package ShiXin;

import java.util.*;

public class DoubleColorBall {
    public static void main(String[] args) {

        int[] winningNumbers = generateLotteryNumbers();

        System.out.print("本期中奖号码：红球：");
        for (int i = 0; i < 6; i++) {
            System.out.print(winningNumbers[i] + " ");
        }
        System.out.println("蓝球：" + winningNumbers[6]);

        int[] userNumbers = getUserInput();

        int redMatchCount = countRedMatches(winningNumbers, userNumbers);
        int blueMatch = winningNumbers[6] == userNumbers[6] ? 1 : 0;

        System.out.println("命中红球数：" + redMatchCount);
        System.out.println("命中蓝球数：" + blueMatch);

        String prizeLevel = determinePrizeLevel(redMatchCount, blueMatch);
        System.out.println("中奖级别：" + prizeLevel);
    }

    public static int[] generateLotteryNumbers() {
        Random random = new Random();
        int[] numbers = new int[7];

        Set<Integer> redNumbers = new LinkedHashSet<>();
        while (redNumbers.size() < 6) {
            redNumbers.add(random.nextInt(33) + 1);
        }
        int index = 0;
        for (int num : redNumbers) {
            numbers[index++] = num;
        }

        numbers[6] = random.nextInt(16) + 1;

        return numbers;
    }

    public static int[] getUserInput() {
        Scanner scanner = new Scanner(System.in);
        int[] userNumbers = new int[7];
        Set<Integer> redSet = new HashSet<>();

        System.out.println("请依次输入 6 个不重复的 1 - 33 之间的红球号码：");
        for (int i = 0; i < 6; i++) {
            int red;
            while (true) {
                System.out.print("第 " + (i + 1) + " 个红球号码：");
                red = scanner.nextInt();
                if (red >= 1 && red <= 33 && !redSet.contains(red)) {
                    redSet.add(red);
                    break;
                } else {
                    System.out.println("输入无效，请输入 1 - 33 之间且不重复的号码。");
                }
            }
            userNumbers[i] = red;
        }

        int blue;
        while (true) {
            System.out.println("请输入 1 - 16 之间的蓝球号码：");
            blue = scanner.nextInt();
            if (blue >= 1 && blue <= 16) {
                userNumbers[6] = blue;
                break;
            } else {
                System.out.println("输入无效，请输入 1 - 16 之间的号码。");
            }
        }

        scanner.close();
        return userNumbers;
    }

    public static int countRedMatches(int[] winningNumbers, int[] userNumbers) {
        int count = 0;
        Set<Integer> winningRedSet = new HashSet<>();
        for (int i = 0; i < 6; i++) {
            winningRedSet.add(winningNumbers[i]);
        }
        for (int i = 0; i < 6; i++) {
            if (winningRedSet.contains(userNumbers[i])) {
                count++;
            }
        }
        return count;
    }

    public static String determinePrizeLevel(int redMatch, int blueMatch) {
        if (redMatch == 6 && blueMatch == 1) {
            return "一等奖";
        } else if (redMatch == 6 && blueMatch == 0) {
            return "二等奖";
        } else if (redMatch == 5 && blueMatch == 1) {
            return "三等奖";
        } else if ((redMatch == 5 && blueMatch == 0) || (redMatch == 4 && blueMatch == 1)) {
            return "四等奖";
        } else if ((redMatch == 4 && blueMatch == 0) || (redMatch == 3 && blueMatch == 1)) {
            return "五等奖";
        } else if ((redMatch == 2 && blueMatch == 1) || (redMatch == 1 && blueMatch == 1) || (redMatch == 0 && blueMatch == 1)) {
            return "六等奖";
        } else {
            return "未中奖";
        }
    }
}